package com.iot.framework.rabbitmq.service;

import com.alibaba.fastjson2.JSONObject;
import com.iot.common.utils.StringUtils;
import com.iot.common.utils.bean.BeanUtils;
import com.iot.system.domain.DongAnOldDeviceInfo;
import com.iot.system.domain.vo.DongAnOldDeviceInfoVo;
import com.iot.system.service.TMDongAnOldDeviceInfoService;
import com.iot.system.vo.DeviceYieldVo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class InfoDumpStorageService {

    @Resource
    private TMDongAnOldDeviceInfoService tMDongAnOldDeviceInfoService;



    @Async
    public void infoDumpStorage(String message) {
        System.out.println("infoDumpStorage:" + message);

        if (StringUtils.contains(message, "active")) {
            return;
        }

        DongAnOldDeviceInfoVo dongAnOldDeviceInfoVo = JSONObject.parseObject(message, DongAnOldDeviceInfoVo.class);

        System.out.println(dongAnOldDeviceInfoVo);

        List<DongAnOldDeviceInfo> list = dongAnOldDeviceInfoVo.getItems().stream().map(item -> {
            DongAnOldDeviceInfo dongAnOldDeviceInfo = new DongAnOldDeviceInfo();
            BeanUtils.copyProperties(item, dongAnOldDeviceInfo);
            dongAnOldDeviceInfo.setBatchNo(dongAnOldDeviceInfoVo.getBatchNo());
            dongAnOldDeviceInfo.setBeginTime(dongAnOldDeviceInfoVo.getBeginTime());
            dongAnOldDeviceInfo.setEndTime(dongAnOldDeviceInfoVo.getEndTime());
            dongAnOldDeviceInfo.setDescription(dongAnOldDeviceInfoVo.getDescription());
            return dongAnOldDeviceInfo;
        }).collect(Collectors.toList());

        tMDongAnOldDeviceInfoService.insertDongAnOldDeviceInfoList(list);
    }


}
